import store from "../store";
//  封装  zanReducer
//  action 行为
// {type:"",payload:1}

//  action.type 在整个 store 中是唯一的
export default function zanReducer(init=0,action){
    switch(action.type){
        case "zanadd":
            return  init += action.payload;
        case "zanjian":
            return init -= action.payload
        default:
            return init;
    }
}

//封装action 方法,返回是 action对象
//同步新增点赞数
export function zanAddAction(payload){
    return {
        type:"zanadd",
        payload
    }
}

//同步减少点赞数
export function zanJianAction(payload){
    return {
        type:"zanjian",
        payload
    }
}

// 异步新增点赞数
export function asynczanJiaAction(payload){
     store.dispatch((dispatch)=>{
        return setTimeout(()=>{
            dispatch(zanAddAction(payload));
        })
    }) 
}
